<?php
/**
 * Project: Recipe
 * File: SetDietaryNeedsAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-4-21
 * Time: 下午2:15
 */

class SetDietaryNeedsAction extends CommonAction{

	function index(){

		import("ORG.Util.Page");
		$setdietaryneeds = M('SetDietaryNeeds');

		if(isset($_POST['search'])){
			if(!empty($_POST['key'])){
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
					case 3 :
						$datas['alias'] = array('like',"%{$_POST['key']}%");
						break;
				}
				$total = $setdietaryneeds->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));
				$show = $Page->show();
				$list = $setdietaryneeds->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
			}else{
				$this->error('搜索内容不能为空', __APP__.'/SetDietaryNeeds/index');
			}
		}else{
			$total = $setdietaryneeds->count();
			$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
			$show = $Page->show();// 分页显示输出
			$list = $setdietaryneeds->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
		}

		$this->assign('list',$list);// 赋值数据集

		$this->assign('show',$show);// 赋值分页输出

		$this->display('index');
	}

	/**
	 * 指向添加饮食需要界面
	 */
	function add(){
		$this->display();
	}

	/**
	 * 添加饮食需要
	 */
	function insert(){
		$setdietaryneeds = D('SetDietaryNeeds');
		if($data = $setdietaryneeds->create()){
			if($setdietaryneeds->add()){
				$this->success('添加饮食需要成功', __APP__.'/SetRecipeInfo/index');
			}else{
				$this->error('添加饮食需要失败');
			}
		}else{
			$this->error($setdietaryneeds->getError(), __APP__.'/SetDietaryNeeds/add');
		}
	}

	/**
	 * 指向修改饮食需要界面
	 */
	function edit(){
		if(isset($_GET['id']) && !empty($_GET['id'])){
			$setdietaryneeds = M('SetDietaryNeeds');
			$data = $setdietaryneeds->find($_GET['id']);
			$this->assign('data', $data);
			$this->display();
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 修改饮食需要
	 */
	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
			$setdietaryneeds = M('SetDietaryNeeds');
			if($data = $setdietaryneeds->create()){
				if($setdietaryneeds->save()){
					$this->success('修改饮食需要成功',  __APP__.'/SetRecipeInfo/index');
				}else{
					$this->error('修改饮食需要失败');
				}
			}else{
				$this->error($setdietaryneeds->getError());
			}
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 删除饮食需要
	 */
	function del(){
		if(!empty($_POST['actions']) || !empty($_GET['id'])){
			$setdietaryneeds = M('SetDietaryNeeds');
			if(isset($_GET['id']) && !empty($_GET['id'])){
				$num = $setdietaryneeds->delete($_GET['id']);
			}else{
				foreach($_POST['actions'] as $key=>$value){
					$num = $setdietaryneeds->delete($value);
				}
			}
			if($num>0){
				$this->success('删除饮食需要成功', __APP__.'/SetRecipeInfo/index');
			}else{
				$this->error('删除饮食需要失败');
			}
		}else{
			$this->error('请先选择饮食需要');
		}
	}


}